﻿Public Class frmQuickVisit
    ' ***************************************************************
    ' ********************** Private Functions **********************
    ' ***************************************************************

    Private Function GetReadyToAddNewItem() As Boolean
        ' This function will give a new Employee ID Randomly.
        ' Then it get ready to start filling in data. It will
        ' also disable the button so the user cannot try to
        ' save anything without entering the information first.

        Try
            Me.VisitBindingSource.AddNew()
            ' Fill current date and time
            Me.HourTextBox.Text = Date.Now.ToLocalTime()
            ' Generate a new Survey ID
            Me.VisitIDTextBox.Text = GenerateRandomNumber(100, 9999999).ToString
            ' Disables the add survey button to make sure the user add values
            btnQuickVisitAdd.Enabled = False
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    ' This turns on/off all input boxes

    Private Function ChangeAllInputBoxesStatus(ByVal NewStatus As Boolean) As Boolean
        Try
            VisitIDTextBox.Enabled = NewStatus
            CustomerNameTextBox.Enabled = NewStatus
            CustomerPhoneMaskedTextBox.Enabled = NewStatus
            EmployeeIDComboBox.Enabled = NewStatus
            HourTextBox.Enabled = NewStatus
            CommentsTextBox.Enabled = NewStatus
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function AddNewItem() As Boolean
        Try
            Me.Validate()
            Me.Atlantic_toyota_atm_dbDataSet.BeginInit()
            Me.VisitBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Atlantic_toyota_atm_dbDataSet)
            Me.Atlantic_toyota_atm_dbDataSet.EndInit()
            MessageBox.Show("Changes Saved.", "Success", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function ValidateQuickVisit() As Boolean
        Try

            If VisitIDTextBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If

            If CustomerNameTextBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If

            If CustomerPhoneMaskedTextBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If

            If EmployeeIDComboBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If

            If HourTextBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If

            If CommentsTextBox.Text.Length = 0 Then
                btnQuickVisitAdd.Enabled = False
            Else
                btnQuickVisitAdd.Enabled = True
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
        Return True
    End Function

    ' ***************************************************************
    ' ************************ Private  Subs ************************
    ' ***************************************************************

    ' Load Form and get ready

    Private Sub frmQuickVisit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            ' Make sure the user won't mess with this boxes
            VisitIDTextBox.ReadOnly = True
            HourTextBox.ReadOnly = True

            ' Hide all labels for the main menu
            lblHome.Visible = False
            'lblVisit.Visible = False
            lblSurvey.Visible = False
            lblStatistics.Visible = False
            lblInventory.Visible = False
            lblEmployee.Visible = False
            lblClose.Visible = False

            ' Load data into textboxes
            Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)

            ' Get ready for editing
            lblSave.Text = "Add New"
            btnQuickVisitAdd.Image = GeneralImageSet.Images("Add")
            btnQuickVisitAdd.Enabled = True
            ChangeAllInputBoxesStatus(False)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ' Ensures there is data in the input boxes

    Private Sub CustomerNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerNameTextBox.TextChanged
        ValidateQuickVisit()
    End Sub

    Private Sub CustomerPhoneMaskedTextBox_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles CustomerPhoneMaskedTextBox.MaskInputRejected
        ValidateQuickVisit()
    End Sub

    Private Sub EmployeeIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeIDComboBox.SelectedIndexChanged
        ValidateQuickVisit()
    End Sub

    Private Sub VisitIDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisitIDTextBox.TextChanged
        ValidateQuickVisit()
    End Sub

    Private Sub HourTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HourTextBox.TextChanged
        ValidateQuickVisit()
    End Sub

    Private Sub CommentsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CommentsTextBox.TextChanged
        ValidateQuickVisit()
    End Sub

    ' Closes all forms

    Private Sub btnQuickVisitClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CloseAllForms()
    End Sub

    ' Adds the data


    Private Sub btnQuickVisitAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuickVisitAdd.Click
        Dim MsgResultVisitDetails As MsgBoxResult
        Try
            If lblSave.Text = "Add New" Then

                ' If I am here is because I just came to the form
                ' or because I just saved a value and is starting
                ' up again. So we will enable the text boxes and 
                ' change the button label to Save to make it 
                ' consistent.

                btnQuickVisitAdd.Image = GeneralImageSet.Images("Save")
                lblSave.Text = "Save"
                ChangeAllInputBoxesStatus(True)
                GetReadyToAddNewItem()
                CustomerNameTextBox.Focus()

            ElseIf lblSave.Text = "Save" Then

                ' If I am here is because Im about to add a value 
                ' So we will disable the text boxes and change 
                ' the button label to Add New to make it consistent.

                btnQuickVisitAdd.Image = GeneralImageSet.Images("Add")
                lblSave.Text = "Add New"
                ChangeAllInputBoxesStatus(False)
                AddNewItem()

                MsgResultVisitDetails = MessageBox.Show("Do you want to add details for this visit?", _
                                                        "Question", MessageBoxButtons.YesNo, _
                                                         MessageBoxIcon.Question)

                If MsgResultVisitDetails = MsgBoxResult.Yes Then
                    SharedVisitId = VisitIDTextBox.Text
                    SwitchForm(My.Forms.frmDetailedVisit)
                End If

                lblVisit.Focus()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnHome_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnClose_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim Confirmation As MsgBoxResult
        Confirmation = MessageBox.Show("Unsaved changes will be lost. Are you sure you want to exit?", _
                                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Confirmation = MsgBoxResult.Yes Then
            CloseAllForms()
        End If
    End Sub
    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnSurvey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSurvey.Click
        SwitchForm(My.Forms.frmSurvey)
    End Sub

    Private Sub btnStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        SwitchForm(My.Forms.frmStatistics)
    End Sub

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub

    '************************************************
    ' Show the label for the items on the main menu *
    '************************************************

    Private Sub btnHome_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnHome.MouseHover
        lblHome.Visible = True
    End Sub

    'Private Sub btnVisit_MouseOver(ByVal sender As System.Object, _
    '              ByVal e As System.EventArgs) Handles btnVisit.MouseHover
    '    lblVisit.Visible = True
    'End Sub

    Private Sub btnSurvey_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseHover
        lblSurvey.Visible = True
    End Sub

    Private Sub btnStatistics_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseHover
        lblStatistics.Visible = True
    End Sub

    Private Sub btnInventory_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseHover
        lblInventory.Visible = True
    End Sub

    Private Sub btnEmployee_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseHover
        lblEmployee.Visible = True
    End Sub

    Private Sub btnClose_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseHover
        lblClose.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************


    Private Sub btnHome_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnHome.MouseLeave
        lblHome.Visible = False
    End Sub

    Private Sub btnSurvey_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseLeave
        lblSurvey.Visible = False
    End Sub

    Private Sub btnStatistics_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseLeave
        lblStatistics.Visible = False
    End Sub

    Private Sub btnInventory_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseLeave
        lblInventory.Visible = False
    End Sub

    Private Sub btnEmployee_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseLeave
        lblEmployee.Visible = False
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        lblClose.Visible = False
    End Sub

End Class